package leetcode.f1t100;

/**
 * 输出最后一个单词的长度
 * https://leetcode.com/problems/length-of-last-word/
 *
 * @Author lichongxiao
 * @Date 2022/10/25 14:13
 */
public class Q58_LengthOfLastWord {
    public int lengthOfLastWord(String s) {
        int length = s.length(), count = 0;
        for (int i = length-1; i > 0; i--) {
            if(s.charAt(i) != ' '){
                count++;
            }else {
                if(count > 0 ){
                    return count;
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {
        String[] s1 = "hello world    ".split(" ");
        for(String s :s1){
            System.out.println(s);
        }
        System.out.println(s1[s1.length-1].length());
    }
}
